package novanok.com.tracktweet;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

public class MapReduceCantidadTweets extends Thread {

    private double MINUTOS = 1;
    private long tiempoEspera = 0;
    private Map<String, Integer> cantidadTweets = new TreeMap<String, Integer>();
    private SimpleDateFormat df = new SimpleDateFormat("HH:mm");
    private boolean isProcessing = true;
    private MainActivity main;
    private int cantBar = 0;

    public boolean isProcessing() {
        return isProcessing;
    }

    public void setProcessing(boolean isProcessing) {
        this.isProcessing = isProcessing;
    }

    public MapReduceCantidadTweets(MainActivity main){
        this.main = main;
        this.tiempoEspera = (long)( MINUTOS * (double)60 * (double)1000 );
        this.start();
    }

    public void run(){
        isProcessing = true;
        while (isProcessing()) {
            try {
                if (cantBar > 7) {
                    clear();
                }

                Date now = new Date();

                int cantidad = this.main.getCantidad_tweets();

                if ( cantidad > 0) {
                    this.cantidadTweets.put(df.format(now), cantidad);
                    this.main.clearCharts();
                }

                Thread.sleep( this.tiempoEspera );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized Map<String, Integer> getCantidadTweets(){
        return this.cantidadTweets;
    }

    public void clear() {
        cantidadTweets.clear();
    }
}
